package com.ruoyi.web.controller.linkage.linkageStrategy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.linkage.Dto.LinkageStrategyListDto;
import com.ruoyi.linkage.Vo.LinkageStrategyAddVo;
import com.ruoyi.linkage.Vo.LinkageStrategyUpdateVo;
import com.ruoyi.linkage.Vo.LinkageStrategyVo;
import com.ruoyi.linkage.domain.LinkageAction;
import com.ruoyi.linkage.domain.LinkageStrategy;
import com.ruoyi.linkage.mapper.LinkageStrategyMapper;
import com.ruoyi.linkage.service.ILinkageActionService;
import com.ruoyi.linkage.service.ILinkageStrategyService;
import com.ruoyi.system.domain.SysDict;
import com.ruoyi.system.domain.SysDictItem;
import com.ruoyi.system.service.ISysDictItemService;
import com.ruoyi.system.service.ISysDictService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.management.MonitorInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 联动策略Controller
 *
 * @author pengjie
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/linkage/strategy")
public class LinkageStrategyController extends BaseController
{
    @Autowired
    private ILinkageStrategyService linkageStrategyService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private ISysDictItemService sysDictItemService;
    /**
     * 查询AI联动策略列表
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:list')")
    @GetMapping("/aiList")
    public TableDataInfo ailist(LinkageStrategyVo linkageStrategyVo)
//    public PageInfo<LinkageStrategy> aiList(LinkageStrategyVo linkageStrategyVo)
    {
        linkageStrategyVo.setAi("1");
        Integer pageNum = Integer.parseInt(linkageStrategyVo.getPageNum());
        Integer pageSize = Integer.parseInt(linkageStrategyVo.getPageSize());
        List<LinkageStrategyListDto> dtoList=linkageStrategyService.getList(linkageStrategyVo);
        int num = dtoList.size();
        dtoList = dtoList.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(dtoList);
        rspData.setTotal(num);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @PreAuthorize("@ss.hasPermi('linkage:strategy:list')")
    @GetMapping("/list")
    public TableDataInfo list(LinkageStrategyVo linkageStrategyVo)
//    public PageInfo<LinkageStrategy> list(LinkageStrategyVo linkageStrategyVo)
    {
//        PageHelper.startPage(Integer.parseInt(linkageStrategyVo.getPageNum()),Integer.parseInt(linkageStrategyVo.getPageSize()));
        linkageStrategyVo.setAi("0");
        Integer pageNum = Integer.parseInt(linkageStrategyVo.getPageNum());
        Integer pageSize = Integer.parseInt(linkageStrategyVo.getPageSize());
        List<LinkageStrategyListDto> dtoList=linkageStrategyService.getList(linkageStrategyVo);
        int num = dtoList.size();
        dtoList = dtoList.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(dtoList);
        rspData.setTotal(num);
        rspData.setMsg("查询成功");
        return rspData;
    }
    /**
     * 修改(停用<--->启用)
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:edit')")
    @Log(title = "联动策略", businessType = BusinessType.UPDATE)
    @PutMapping("/swatchStrategy")
    public AjaxResult editStartAndEnd(@RequestBody LinkageStrategy linkageStrategy)
    {
        return toAjax(linkageStrategyService.updateLinkageStrategy(linkageStrategy));
    }

    /**
     * 导出联动策略列表
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:export')")
    @Log(title = "联动策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LinkageStrategy linkageStrategy)
    {
        List<LinkageStrategy> list = linkageStrategyService.selectLinkageStrategyList(linkageStrategy);
        ExcelUtil<LinkageStrategy> util = new ExcelUtil<LinkageStrategy>(LinkageStrategy.class);
        util.exportExcel(response, list, "联动策略数据");
    }

    /**
     * 获取联动策略详细信息
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:query')")
    @GetMapping(value = "/{linkageStrategyId}")
    public AjaxResult getInfo(@PathVariable("linkageStrategyId") String linkageStrategyId)
    {
        return success(linkageStrategyService.selectLinkageStrategyByLinkageStrategyId(linkageStrategyId));
    }

    /**
     * 新增联动策略
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:add')")
    @Log(title = "联动策略", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LinkageStrategyAddVo linkageStrategyAddVo)
    {
        linkageStrategyAddVo.setAi("0");
        return toAjax(linkageStrategyService.addLinkageStrategy(linkageStrategyAddVo));
    }
    /**
     * 新增联动策略
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:add')")
    @Log(title = "联动策略", businessType = BusinessType.INSERT)
    @PostMapping("/aiAdd")
    public AjaxResult aiAdd(@RequestBody LinkageStrategyAddVo linkageStrategyAddVo)
    {
        linkageStrategyAddVo.setAi("1");
        return toAjax(linkageStrategyService.addLinkageStrategy(linkageStrategyAddVo));
    }
    /**
     * 修改联动策略
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:edit')")
    @Log(title = "联动策略", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LinkageStrategyUpdateVo linkageStrategyUpdateVo)
    {
        linkageStrategyUpdateVo.setAi("0");
        return toAjax(linkageStrategyService.editLinkageStrategy(linkageStrategyUpdateVo));
    }

    /**
     * 修改联动策略
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:edit')")
    @Log(title = "联动策略", businessType = BusinessType.UPDATE)
    @PutMapping("/aiEdit")
    public AjaxResult AiEdit(@RequestBody LinkageStrategyUpdateVo linkageStrategyUpdateVo)
    {
        linkageStrategyUpdateVo.setAi("1");
        return toAjax(linkageStrategyService.editLinkageStrategy(linkageStrategyUpdateVo));
    }
    /**
     * 删除联动策略
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:remove')")
    @Log(title = "联动策略", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete/{linkageStrategyIds}")
    public AjaxResult remove(@PathVariable String[] linkageStrategyIds)
    {
        return toAjax(linkageStrategyService.deleteLinkageStrategyByLinkageStrategyIds(linkageStrategyIds));
    }

    /**
     * 删除AI联动策略
     */
    @PreAuthorize("@ss.hasPermi('linkage:strategy:remove')")
    @Log(title = "联动策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/Aidelete/{linkageStrategyIds}")
    public AjaxResult removeAi(@PathVariable String[] linkageStrategyIds)
    {
        return toAjax(linkageStrategyService.deleteLinkageStrategyByLinkageStrategyIds(linkageStrategyIds));
    }

    /**
     * 查询告警级别
     */
    @PreAuthorize("@ss.hasPermi('dangerControl:dict:list')")
    @GetMapping("/queryOperateDict")
    public AjaxResult list(SysDict sysDict)
    {
        sysDict.setDictCode("alert_level");
        List<SysDict> sysDictList=sysDictService.selectSysDictList(sysDict);
        SysDictItem sysDictItem=new SysDictItem();
        sysDictItem.setDictId(sysDictList.get(0).getId());
        List<SysDictItem> sysDictItems=sysDictItemService.selectSysDictItemList(sysDictItem);
        return success(sysDictItems);
    }
}
